package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.Trace;
import com.zsyh.jiujiu.adepter.WuliuAdepter;
import com.zsyh.jiujiu.bean.RetrofitWuliu;
import com.zsyh.jiujiu.bean.SeeekBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SeekWuliuActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.wuliulist)
    RecyclerView wuliulist;

    private List<Trace> traceList = new ArrayList<>(10);
    private WuliuAdepter adapter;
    private String gongsi,code,orderno;

    @Override
    protected int initLayout() {
        return R.layout.activity_seekwuliu;
    }

    @Override
    protected void initView() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thisActivity.finish();
            }
        });
    }

    @Override
    protected void initData() {
        gongsi=getIntent().getStringExtra("gongsi");
        code=getIntent().getStringExtra("code");
        orderno=getIntent().getStringExtra("orderno");
        if(!code.equals("null")){
            getWuliuinfo(code, orderno);
        }


    }

    @Override
    protected void initEvent() {

    }

    public void getWuliuinfo(String names, String number) {
        Api dataApi = RetrofitWuliu.getInstance().getRetrofit().create(Api.class);
        dataApi.wuliu(names, number, "9a4090e5886ecd9fe24c1926053d315a")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            String body=responseBody.string();
                            Log.e("wuliuxinxi",body+"");
                            Gson gson=new Gson();
                            SeeekBean myWulliuBean=gson.fromJson(body,SeeekBean.class);
                            for(int i=0;i<myWulliuBean.getResult().getList().size();i++){
                                traceList.add(new Trace(0,myWulliuBean.getResult().getList().get(i).getDatetime(), myWulliuBean.getResult().getList().get(i).getRemark()));
                            }
                            Collections.reverse(traceList);
                            adapter = new WuliuAdepter(thisActivity, traceList);
                            wuliulist.setLayoutManager(new LinearLayoutManager(thisActivity));
                            wuliulist.setAdapter(adapter);
                            Log.e("wuliu",body);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("wuliuerror", e.getMessage());

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
